<template>
  <view class="search-container">
    <!-- 头部部分 -->
    <view class="search-box">
      <uni-search-bar
        @input="input"
        placeholder="请输入搜索内容"
        :radius="100"
        cancelButton="none"
        @clear="qsearch = []"
      />
    </view>
    <view class="sugg-list" v-if="qsearch.length > 0">
      <view
        class="sugg-item"
        v-for="item in qsearch"
        @click="goToDetail(item.goods_id)"
      >
        <text class="goods-name">{{ item.goods_name }}</text>
      </view>
    </view>
    <view class="history-box" v-else>
      <view class="history-title">
        <text>搜索历史</text>
        <uni-icons type="trash" size="17" @click="clearHistory"></uni-icons>
      </view>
      <view class="history-list">
        <uni-tag
          v-for="item in seaechList"
          :key="item"
          :text="item"
          @click="goToGoodsList(item)"
        ></uni-tag>
      </view>
    </view>
  </view>
</template>

<script>
import uniIcons from "../../components/uni-icons/uni-icons.vue";

export default {
  components: { uniIcons },
  data() {
    return {
      // 搜索到的商品列表数据数组
      qsearch: [],
      // 搜索历史列表数据数组
      seaechList: [],
    };
  },
  onLoad() {
    // 第一次进入页面的时候，我们需要把本地的历史赋值给 seaechList
    this.seaechList = uni.getStorageSync("history") || [];
  },
  methods: {
    input(val) {
      clearTimeout(this.timer);
      this.timer = setTimeout(() => {
        if (val.trim().length === 0) {
          this.qsearch = [];
          return;
        }
        this.search(val);
      }, 500);
    },
    // 发请求获取数据
    async search(val) {
      const res = await uni.$request({
        url: "goods/qsearch",
        data: {
          query: val,
        },
      });
      // 保存搜索历史
      // console.log(res);
      this.saveSearch(val);
      if (res.meta.status === 200) {
        this.qsearch = res.message;
      }
    },
    // 保存搜索历史
    saveSearch(val) {
      // 判断val在数组中是否存在，如果存在，则干掉
      const findIndex = this.seaechList.findIndex((item) => item === val);
      if (findIndex > -1) {
        this.seaechList.splice(findIndex, 1);
      }
      // 对数组倒序，去重显示
      this.seaechList.unshift(val);
      // 保存到本地
      uni.setStorageSync("history", this.seaechList);
    },
    // 跳转到商品详情页面
    goToDetail(goods_id) {
      uni.navigateTo({
        url: "/subpkg/goods-detail/index?goods_id=" + goods_id,
      });
    },
    // 跳转到商品列表页
    goToGoodsList(query) {
      uni.navigateTo({
        url: "/subpkg/goods-list/index?query" + query,
      });
    },
    // 清除搜索历史
    clearHistory() {
      this.seaechList = [];
      // 清除本地
      uni.setStorageSync("history", []);
    },
  },
};
</script>

<style lang="scss" scoped>
.search-container {
  .search-box {
    position: sticky;
    top: 0;
    z-index: 999;
    ::v-deep .uni-searchbar {
      /* #ifndef APP-NVUE */
      display: flex;
      /* #endif */
      flex-direction: row;
      position: relative;
      padding: 16rpx;
      /* 将默认的 #FFFFFF 改为 #C00000 */
      background-color: #c00000;
    }
  }

  .history-box {
    padding: 0 5px;

    .history-title {
      display: flex;
      justify-content: space-between;
      align-items: center;
      height: 40px;
      font-size: 13px;
      border-bottom: 1px solid #efefef;
    }

    .history-list {
      display: flex;
      flex-wrap: wrap;

      ::v-deep .uni-tag {
        margin-top: 5px;
        margin-right: 5px;
      }
    }
  }

  .sugg-list {
    padding: 0 5px;

    .sugg-item {
      font-size: 12px;
      padding: 13px 0;
      border-bottom: 1px solid #efefef;
      display: flex;
      align-items: center;
      justify-content: space-between;

      .goods-name {
        // 文字不允许换行（单行文本）
        white-space: nowrap;
        // 溢出部分隐藏
        overflow: hidden;
        // 文本溢出后，使用 ... 代替
        text-overflow: ellipsis;
        margin-right: 3px;
      }
    }
  }
}
</style>
